//
//  HRZCenterItemCell.m
//  EMarket
//
//  Created by 石头 on 2018/5/16.
//  Copyright © 2018年 好日子. All rights reserved.
//

#import "HRZCenterItemCell.h"
// Controllers

// Models
#import "HRZLocalOrderStatusModel.h"
// Views
#import "HRZStateItemCell.h"
#import "HRZStateItemFooterView.h"
// Vendors
#import <MJExtension.h>
#import "HRZTool.h"
// Categories

// Others

@interface HRZCenterItemCell()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray<HRZLocalOrderStatusModel*> *stateItem;
@end

static NSString * const HRZStateItemCellID = @"HRZStateItemCell";
static NSString * const HRZStateItemFooterViewID = @"HRZStateItemFooterView";

@implementation HRZCenterItemCell

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *dcFlowLayout = [UICollectionViewFlowLayout new];
        dcFlowLayout.minimumLineSpacing = dcFlowLayout.minimumInteritemSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:dcFlowLayout];
        
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        
        //注册Cell
        [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([HRZStateItemCell class]) bundle:nil] forCellWithReuseIdentifier:HRZStateItemCellID];
        //注册footerView
        [_collectionView registerClass:[HRZStateItemFooterView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:HRZStateItemFooterViewID];
        
        [self addSubview:_collectionView];
    }
    return _collectionView;
}

- (NSArray<HRZLocalOrderStatusModel *> *)stateItem
{
    if (!_stateItem) {
        _stateItem = [NSArray array];
    }
    return _stateItem;
}

#pragma mark - Intial
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI
{
    self.backgroundColor = [UIColor whiteColor];
    self.collectionView.backgroundColor = HRZBGColor;
    _stateItem = [HRZTool getLocalFileOrderStatusInfo];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
}


#pragma mark - <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _stateItem.count;
}


#pragma mark - <UICollectionViewDelegate>
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HRZStateItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HRZStateItemCellID forIndexPath:indexPath];
    cell.stateItem = _stateItem[indexPath.row];//赋值
    
    return cell;
}


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableView = nil;
    if (kind == UICollectionElementKindSectionFooter) {
        HRZStateItemFooterView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:HRZStateItemFooterViewID forIndexPath:indexPath];
        reusableView = footer;
    }
    return reusableView;
}

#pragma mark - <UICollectionViewDelegateFlowLayout>
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(kScreenWidth / 5, 90);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    return CGSizeMake(kScreenWidth, 0);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.delegate respondsToSelector:@selector(delegateTableCell:didSelectedItemAtIndexPath:)]) {
        [self.delegate delegateTableCell:self didSelectedItemAtIndexPath:indexPath];
    }
}

@end
